package com.pingtai.indent.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.pingtai.filter.ChildUserFilter;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentSearch;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class TaobaoIndentAction {

	private UserBaseIndentType baseIndentType;
	private WeiboIndentService weiboIndentService;
	private String base;
	private UserManagerService userManagerService;
	private User curUser;
	private Integer currentPage = 1;
	private Integer pageNum;
	private PaginationSupport paginationSupport;
	private String current;
	private UserIndentSearch qe;
	private String queryString;
	private Float price;
	private UserIndent userIndent;
	private List baseIndentTypeList;
	private List indentTypeList;
	private List priceList;
	private Integer size;
	private List baseIndentTypeMinAmountList;
	private List baseIndentTypeMaxAmountList;
	private List baseIndentTypeUnitPriceList;
	private List baseIndentTypeNameList;
	private List baseIndentTypeUnitNameList;
	private List indentNumberTmpList;
private String typeRadio;
	
	public String getTypeRadio() {
		return typeRadio;
	}
	public void setTypeRadio(String typeRadio) {
		this.typeRadio = typeRadio;
	}
	

	public List getIndentNumberTmpList() {
		return indentNumberTmpList;
	}

	public void setIndentNumberTmpList(List indentNumberTmpList) {
		this.indentNumberTmpList = indentNumberTmpList;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public List getBaseIndentTypeMinAmountList() {
		return baseIndentTypeMinAmountList;
	}

	public void setBaseIndentTypeMinAmountList(List baseIndentTypeMinAmountList) {
		this.baseIndentTypeMinAmountList = baseIndentTypeMinAmountList;
	}

	public List getBaseIndentTypeMaxAmountList() {
		return baseIndentTypeMaxAmountList;
	}

	public void setBaseIndentTypeMaxAmountList(List baseIndentTypeMaxAmountList) {
		this.baseIndentTypeMaxAmountList = baseIndentTypeMaxAmountList;
	}

	public List getBaseIndentTypeUnitPriceList() {
		return baseIndentTypeUnitPriceList;
	}

	public void setBaseIndentTypeUnitPriceList(List baseIndentTypeUnitPriceList) {
		this.baseIndentTypeUnitPriceList = baseIndentTypeUnitPriceList;
	}

	public List getBaseIndentTypeNameList() {
		return baseIndentTypeNameList;
	}

	public void setBaseIndentTypeNameList(List baseIndentTypeNameList) {
		this.baseIndentTypeNameList = baseIndentTypeNameList;
	}

	public List getBaseIndentTypeUnitNameList() {
		return baseIndentTypeUnitNameList;
	}

	public void setBaseIndentTypeUnitNameList(List baseIndentTypeUnitNameList) {
		this.baseIndentTypeUnitNameList = baseIndentTypeUnitNameList;
	}

	public List getBaseIndentTypeList() {
		return baseIndentTypeList;
	}

	public void setBaseIndentTypeList(List baseIndentTypeList) {
		this.baseIndentTypeList = baseIndentTypeList;
	}

	public List getIndentTypeList() {
		return indentTypeList;
	}

	public void setIndentTypeList(List indentTypeList) {
		this.indentTypeList = indentTypeList;
	}

	public List getPriceList() {
		return priceList;
	}

	public void setPriceList(List priceList) {
		this.priceList = priceList;
	}

	public UserBaseIndentType getBaseIndentType() {
		return baseIndentType;
	}

	public void setBaseIndentType(UserBaseIndentType baseIndentType) {
		this.baseIndentType = baseIndentType;
	}

	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}

	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}

	public String getBase() {
		return base;
	}

	public void setBase(String base) {
		this.base = base;
	}

	public UserManagerService getUserManagerService() {
		return userManagerService;
	}

	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public Integer getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}

	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}

	public String getCurrent() {
		return current;
	}

	public void setCurrent(String current) {
		this.current = current;
	}

	public UserIndentSearch getQe() {
		return qe;
	}

	public void setQe(UserIndentSearch qe) {
		this.qe = qe;
	}

	public String getQueryString() {
		return queryString;
	}

	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}

	public Float getPrice() {
		return price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

	public UserIndent getUserIndent() {
		return userIndent;
	}

	public void setUserIndent(UserIndent userIndent) {
		this.userIndent = userIndent;
	}

	public String taobaoShoucangManageUI() {
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		
		curUser = userManagerService.findCurrectUser();
		if (currentPage > 0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE * (currentPage - 1);
		}

		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while (childIt.hasNext()) {
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set ch = ubt.getChildrenBaseIndentType();
			Iterator chIt = ch.iterator();
			while (chIt.hasNext()) {
				UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
				baseList.add(ubt2.getId());
			}
		}
		baseList.add(baseIndentType.getId());
		
		if(userManagerService.isAudit(curUser)){
			List produceList = userManagerService.getProduceList2();
			int len = baseList.size();
			int i=0;
			for(;i<len;i++){
				Integer b =  (Integer) baseList.get(i);
				boolean f = false;
				for(int j=0;j<produceList.size();j++){
					UserIndentType u = (UserIndentType) produceList.get(j);
					UserBaseIndentType b2 = u.getUserBaseIndentType();
					if(b.equals(b2.getId())){
						f = true;
						break;
					}
				}
				
				if(!f){
					baseList.remove(i);
					i--;
					len--;
				}
			}
		}

		if (userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)) {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, true, baseList);
		} else {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, false, baseList);
		}
		if (qe != null) {
			current = qe.getStatus();
		} else {
			current = "all";
		}

		return "success";
	}

	public String taobaoShoucangManageUIForSearch() {
		baseIndentType = weiboIndentService.getBaseIndentTypeById(new Integer(
				base));

		curUser = userManagerService.findCurrectUser();
		if (currentPage > 0) {
			pageNum = currentPage;
			currentPage = PaginationSupport.PAGESIZE * (currentPage - 1);
		}

		List baseList = new ArrayList();
		Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
		Iterator childIt = childBaseIndentType.iterator();
		while (childIt.hasNext()) {
			UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
			baseList.add(ubt.getId());
			Set ch = ubt.getChildrenBaseIndentType();
			Iterator chIt = ch.iterator();
			while (chIt.hasNext()) {
				UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
				baseList.add(ubt2.getId());
			}
		}
		baseList.add(baseIndentType.getId());
		
		if(userManagerService.isAudit(curUser)){
			List produceList = userManagerService.getProduceList2();
			int len = baseList.size();
			int i=0;
			for(;i<len;i++){
				Integer b =  (Integer) baseList.get(i);
				boolean f = false;
				for(int j=0;j<produceList.size();j++){
					UserIndentType u = (UserIndentType) produceList.get(j);
					UserBaseIndentType b2 = u.getUserBaseIndentType();
					if(b.equals(b2.getId())){
						f = true;
						break;
					}
				}
				
				if(!f){
					baseList.remove(i);
					i--;
					len--;
				}
			}
		}
		if (userManagerService.isNotUser(curUser)&& !userManagerService.isUser_2(curUser)) {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, true, baseList, qe);
		} else {
			paginationSupport = weiboIndentService.getIndentForPage(
					currentPage, false, baseList, qe);
		}
		if (qe != null) {
			current = qe.getStatus();
		} else {
			current = "all";
		}

		return "success";
	}
	
	public String toTaobaoWeiAdd(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base)); 
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				indentType = weiboIndentService.updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		return "success";
	}
	
	public String toTaobaoShoucangAdd(){
		if(ChildUserFilter.getFlag2()==0) {
			return "produceWeihu";
    	}
		baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
		baseIndentTypeList = new ArrayList();
		priceList = new ArrayList();
		
		baseIndentTypeMinAmountList= new ArrayList();
		baseIndentTypeMaxAmountList= new ArrayList();
		baseIndentTypeUnitPriceList= new ArrayList();
		baseIndentTypeNameList= new ArrayList();
		baseIndentTypeUnitNameList= new ArrayList();
		
		
		indentTypeList = new ArrayList();
		if(baseIndentType.getEnable()==0){
			return "produceWeihu";
		}
		
		
		curUser = userManagerService.findCurrectUser();
		if(userManagerService.isRoot(curUser)){
			
			Set s = baseIndentType.getChildrenBaseIndentType();
			Iterator it = s.iterator();
			while(it.hasNext()){
				UserBaseIndentType b = (UserBaseIndentType) it.next();
				if(b.getEnable()!=1) continue;
				baseIndentTypeList.add(b);
			}
			
			if(baseIndentTypeList!=null&&baseIndentTypeList.size()>0){
				for(int i=0;i<baseIndentTypeList.size();i++){
					UserBaseIndentType b = (UserBaseIndentType) baseIndentTypeList.get(i);
					Float p = b.getPrice();
					priceList.add(p);
				}
			}else{
				price = baseIndentType.getPrice();
			}
			
		}else{
			UserIndentType indentType = weiboIndentService.getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				
				Set s2 = indentType.getChildrenIndentType();
				Iterator it2 = s2.iterator();
				while(it2.hasNext()){
					UserIndentType idt = (UserIndentType) it2.next();
					if(idt.getUserBaseIndentType().getEnable()!=1) continue;
					indentTypeList.add(idt);
				}
				
				if(indentTypeList!=null&&indentTypeList.size()>0){
					indentTypeList = Utils.menuSort(indentTypeList);
					for(int i=0;i<indentTypeList.size();i++){
						UserIndentType idt = (UserIndentType) indentTypeList.get(i);
						UserBaseIndentType bdt = idt.getUserBaseIndentType();
						baseIndentTypeList.add(bdt);
						idt = weiboIndentService.updateIndentType(idt);
						Float p = idt.getPrice();
						priceList.add(p);
						baseIndentTypeMinAmountList.add(bdt.getMinAmount());
						baseIndentTypeMaxAmountList.add(bdt.getMaxAmount());
						baseIndentTypeUnitPriceList.add(bdt.getUnit2());
						baseIndentTypeNameList.add(bdt.getName());
						baseIndentTypeUnitNameList.add(bdt.getUnit());
					}
				}else{
					price = indentType.getPrice();
				}
				
				
			}
		}
		return "success";
	}
	
	public String addTaobaoShoucangIndent(){
		//判断价钱是否合法
		User curUser = userManagerService.findCurrectUser();
		boolean flag = weiboIndentService.isLawfulOfPrice(price,curUser,new Integer(base));
		if(flag){
			
			baseIndentType =weiboIndentService.getBaseIndentTypeById(new Integer(base));
			String s = weiboIndentService.saveIndentForTaobaoShoucang(userIndent,curUser,base,baseIndentType);
			return s;
		}else{
			return "add_indent_403";
		}
	}
	
	public String toTaobaoShoucangIndentDetails(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		indentNumberTmpList = weiboIndentService.getIndentNumberTmp(userIndent);
		return "success";
	}
	
	public String toTaobaoShoucangIndentRefund(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String taobaoShoucangIndentRefund(){
		UserIndent userIndent1 = weiboIndentService.getUserIndentById(userIndent.getId());
		userIndent1.setRefundReason(userIndent.getRefundReason());
		User curUser = userManagerService.findCurrectUser();
		userIndent1.setState(userIndent.getState());
		userIndent1.setRefundDate(new Date());
		userIndent1.setRefundUser(curUser);
		weiboIndentService.saveUserIndent(userIndent1);
		UserPay userPay = new UserPay();
		
		User childUser = userIndent1.getUser();
		userPay.setChildUser(childUser);
		userPay.setUser(curUser);
		userPay.setCreateDate(new Date());
		userPay.setMoney(userIndent1.getTotalPrice());
		userPay.setType(1);
		userPay.setUserAllMoney(childUser.getMoney()+userIndent1.getTotalPrice());
		userPay.setUserIndent(userIndent1);
		userPay.setType2(4);
		userPay.setUserBaseIndent(userIndent1.getBaseIndentType());
		userPay.setIndentCreateUser(userIndent1.getUser());
		userManagerService.saveUserPay(userPay);
		childUser.setMoney(childUser.getMoney()+userIndent1.getTotalPrice());
		userManagerService.saveOrUpdateUser(childUser);
		baseIndentType = userIndent1.getBaseIndentType();
		return "success";
	}
	
	public String toTaobaoShoucangIndentComplete(){
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		return "success";
	}
	
	public String taobaoShoucangIndentComplete(){
		int nowNumber = userIndent.getNowNumber();
		userIndent = weiboIndentService.getUserIndentById(userIndent.getId());
		User completeUser = userManagerService.findCurrectUser();
		//调用返利方法
		try {
			userManagerService.userIndentRebate(userIndent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		userIndent.setState(2);
		userIndent.setNowNumber(nowNumber);
		userIndent.setEndDate(new Date());
		userIndent.setEndUser(completeUser);
		weiboIndentService.saveUserIndent(userIndent);
		baseIndentType = userIndent.getBaseIndentType();
		return "success";
	}
}
